﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.InfoConfig;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.InfoConfig;

namespace MINERP.InfoConfig
{
    public partial class FrmModifyProductPrice : Form
    {
        public FrmModifyProductPrice()
        {
            InitializeComponent();
        }
        public FrmModifyProductPrice(ProductDDO inddo)
        {
            InitializeComponent();
            CurrProduct = inddo;
        }
        public FrmModifyProductPrice(Shop_Paper_Price_Info inPaperInfo)
        {
            InitializeComponent();
            currPaperTypeInfo = inPaperInfo;
        }
        public FrmModifyProductPrice(Shop_CommonChargeItem_Price_Info inComminInfo)
        {
            InitializeComponent();
            CurrcommItem = inComminInfo;
        }
        ModifyProductPriceBll _bll = new ModifyProductPriceBll();
        ProductDDO CurrProduct = null;
        Shop_Paper_Price_Info currPaperTypeInfo = null;
        Shop_CommonChargeItem_Price_Info CurrcommItem = null;

        private void FrmModifyProductPrice_Load(object sender, EventArgs e)
        {
            initForm();
        }
        void initForm()
        {
            List<int> levelist = _bll.GetAllLevel();
            foreach (int i in levelist)
            {
                comboBoxShopLevel.Items.Add(i);

            }
            List<string> aresList = _bll.GetAllAresa();
            foreach (string s in aresList)
            {
                comboBoxAreas.Items.Add(s);
            }
            List<string> salesList = _bll.GetAllsaleman();
            foreach (string s in salesList)
            {
                comboBoxSaelsman.Items.Add(s);
            }
            List<tb_CostType_info> listCost = _bll.GetAllCostType();
            comboBoxCostType.DataSource = listCost;
            comboBoxCostType.ValueMember = "CostTypeId";
            comboBoxCostType.DisplayMember = "TypeName";
            //comboBoxPaperType.DataSource = _bll.GetAllPaperType();
            //comboBoxPaperType.DisplayMember = "Name";
            //comboBoxPaperType.ValueMember = "PaperTypeID";


            if (CurrProduct != null && currPaperTypeInfo == null && CurrcommItem == null)
            {
                groupBoxProduct.Enabled = true;
                if (!CurrProduct.IsShopProduct && !CurrProduct.IsShopProduct2)
                {
                    labelControlAction.Text = "删除模式";
                    groupBoxProduct.Enabled = false; ;
                }
                textBoxProductName.Text = CurrProduct.ProductName;
                textBoxWidth.Text = CurrProduct.Width.ToString();
                textBoxHeigth.Text = CurrProduct.Height.ToString();
                textEditSingerPrice.Text = CurrProduct.SinglePrice.ToString("0.00");
                textEditprductUnitPrice.Text = CurrProduct.UnitPrice.ToString("0.00");
                textEditAppendPrice.Text = CurrProduct.AppendPrice.ToString("0.00");
                //textEditUnitPrice.Text = CurrProduct.UnitPrice.ToString("0.00");


            }
            else if (CurrProduct == null && currPaperTypeInfo != null && CurrcommItem == null)
            {
                textBoxProductName.Text = currPaperTypeInfo.PaperName;
                groupBoxPrint.Enabled = true;
                if (!currPaperTypeInfo.isShopPaperPrice&&!currPaperTypeInfo.isShopPaperPrice2)
                {
                    labelControlAction.Text = "删除模式";
                    groupBoxPrint.Enabled = false;
                }
                textEditUnitPrice.Text = currPaperTypeInfo.UnitPrice.ToString("0.00");
            }
            else if (CurrProduct == null && currPaperTypeInfo == null && CurrcommItem != null)
            {
                groupBoxComm.Enabled = true;
                textBoxProductName.Text = CurrcommItem.ItemName;
                if (!CurrcommItem.isShopCommomItem && !CurrcommItem.isShopCommomItem2)
                {
                    labelControlAction.Text = "删除模式";
                    groupBoxComm.Enabled = false;
                }
                textEditSinglePrice.Text = CurrcommItem.SinglePrice.ToString("0.00");
                textEditCommUnitPrice.Text = CurrcommItem.UnitPrice.ToString("0.00");
                TextEditCommAreaPrice.Text = CurrcommItem.AreaPrice.ToString("0.00");
            }
            //if (currPaperTypeInfo != null && currPaperTypeInfo.ShopInfo != null && currPaperTypeInfo.ShopName != null)
            //{
            //    textBoxProductName.Text = currPaperTypeInfo.PaperName;
            //    groupBoxPrint.Enabled = true;
            //    if (!currPaperTypeInfo.isShopPaperPrice && !currPaperTypeInfo.isShopPaperPrice2)
            //    {
            //        labelControlAction.Text = "删除模式";
            //        groupBoxPrint.Enabled = false;
            //    }
            //    textEditUnitPrice.Text = currPaperTypeInfo.UnitPrice.ToString("0.00");
            //}

        }

        private void checkBoxPrint_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            try
            {


                // PaperType_Info papertype = currPaperTypeInfo;
                tb_CostType_info costTypeInfo = (tb_CostType_info)comboBoxCostType.SelectedItem;
                string area = comboBoxAreas.Text;
                string sealMan = comboBoxSaelsman.Text.Trim();
                int shopLevel = Convert.ToInt32(comboBoxShopLevel.Text);
                int intModifyType = 0;//0调整成为统一价格  1统一调整价格变化幅度一样
                int intAdd = 0;//不添加
                if (checkBoxIsAdd.Checked)
                {
                    if (!checkBoxNoUpdate.Checked)
                    {
                        intAdd = 1;///更新+添加
                    }
                    else
                    {
                        intAdd = 2;///只添加不更新
                    }
                }
                else
                {
                    if (!checkBoxNoUpdate.Checked)
                    {
                        intAdd = 0;///更新
                    }
                    else
                    {
                        intAdd = 2;///只添加不更新
                    }
                }


                if (System.Windows.Forms.DialogResult.OK == MessageBox.Show("确定要修改价格", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    this.Cursor = Cursors.WaitCursor;
                    buttonOk.Enabled = false;
                    if (CurrProduct != null && currPaperTypeInfo == null && CurrcommItem==null)//产品
                    {
                        if (radioButtonPrintChangValue.Checked)
                        {
                            intModifyType = 1;
                        }
                        CurrProduct.UnitPrice = Convert.ToDecimal(textEditprductUnitPrice.Text);
                        CurrProduct.SinglePrice = Convert.ToDecimal(textEditSingerPrice.Text);
                        CurrProduct.AppendPrice = Convert.ToDecimal(textEditAppendPrice.Text);



                        int intVal = _bll.UpdateShopProductPrice(CurrProduct, intModifyType, intAdd, area, sealMan, shopLevel, costTypeInfo);
                        if (intVal == 0)
                        {
                            MessageBox.Show("产品价格修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                            this.Close();
                            return;
                        }


                    }
                    else if (CurrProduct == null && currPaperTypeInfo != null && CurrcommItem == null)//输出平米价
                    {
                        if (radioButtonProductChangValue.Checked)
                        {
                            intModifyType = 1;
                        }
                        currPaperTypeInfo.UnitPrice = Convert.ToDecimal(textEditUnitPrice.Text);

                        int intVal = _bll.updatePrintPrice(currPaperTypeInfo, intModifyType, intAdd, area, sealMan, shopLevel, costTypeInfo);
                        if (intVal == 0)
                        {
                            MessageBox.Show("纸型修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                            return;
                        }
                    }
                    else  if (CurrProduct == null && currPaperTypeInfo == null && CurrcommItem != null )//公共类的收费项目
                    {
                        if (radioButtonCommChangValue.Checked)
                        {
                            intModifyType = 1;
                        }
                        CurrcommItem.SinglePrice = Convert.ToDecimal(textEditSinglePrice.Text);
                        CurrcommItem.UnitPrice = Convert.ToDecimal(textEditCommUnitPrice.Text);
                        CurrcommItem.AreaPrice = Convert.ToDecimal(TextEditCommAreaPrice.Text);
                        int intVal = _bll.UpdateCommPrice(CurrcommItem, intModifyType, intAdd, area, sealMan, shopLevel, costTypeInfo);
                        if (intVal == 0)
                        {
                            MessageBox.Show("公共收费项目修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            this.Close();
                            return;
                        }
                    }
                    //if (currPaperTypeInfo != null && currPaperTypeInfo.ShopInfo == null && currPaperTypeInfo.ShopName != null)//产品存在但是商户不存在说明是要删除的纸型收费方式
                    //{
                    //    int intVal = _bll.DelPrintPrice(currPaperTypeInfo, intModifyType, intAdd, area, sealMan, shopLevel);
                    //    if (intVal == 0)
                    //    {
                    //        MessageBox.Show("纸型价格删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //        this.Close();
                    //    }
                    //}
                    //if (CurrcommItem != null && CurrcommItem.Shop == null && CurrcommItem.TypeName != null)//批量取消商户的公共收费项目
                    //{
                    //    int intVal = _bll.DelCommPrice(CurrcommItem, intAdd, area, sealMan, shopLevel);
                    //    if (intVal == 0)
                    //    {
                    //        MessageBox.Show("修改成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //        this.Close();
                    //    }
                    //}

                }
            }
            catch (Exception es)
            {

                MessageBox.Show(es.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
                buttonOk.Enabled = true;
            }

        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radioButtonPrintChangValue_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonPrintChangValue.Checked)
            {
                textEditUnitPrice.Text = "0";
            }

        }

        private void radioButtonALLPrintPrice_CheckedChanged(object sender, EventArgs e)
        {
            textEditUnitPrice.Text = currPaperTypeInfo.UnitPrice.ToString("0.00");
        }

        private void radioButtonProductChangValue_CheckedChanged(object sender, EventArgs e)
        {
            textEditSingerPrice.Text = "0";
            textEditprductUnitPrice.Text = "0";
            textEditAppendPrice.Text = "0";
        }

        private void radioButtonALLPrductPrice_CheckedChanged(object sender, EventArgs e)
        {
            textEditSingerPrice.Text = CurrProduct.SinglePrice.ToString("0.00");
            textEditprductUnitPrice.Text = CurrProduct.UnitPrice.ToString("0.00");
            textEditAppendPrice.Text = CurrProduct.AppendPrice.ToString("0.00");
        }

        private void radioButtonALLCommPrice_CheckedChanged(object sender, EventArgs e)
        {
            textEditSinglePrice.Text = CurrcommItem.SinglePrice.ToString("0.00");
            textEditCommUnitPrice.Text = CurrcommItem.UnitPrice.ToString("0.00");
            TextEditCommAreaPrice.Text = CurrcommItem.AreaPrice.ToString("0.00");
        }

        private void radioButtonCommChangValue_CheckedChanged(object sender, EventArgs e)
        {
            textEditSinglePrice.Text = "0";
            textEditCommUnitPrice.Text = "0";
            TextEditCommAreaPrice.Text = "0";
        }




    }
}
